package com.linktel.productionLine.util;

import com.linktel.productionLine.common.ErrorCode;
import com.linktel.productionLine.exception.BusinessException;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author HuangJiaHui
 * @Date 2023/9/11 19:25
 */
public class MethodUtils {
    public static Calendar getCalendarByDate(String planningDate) {
        DateFormat dateFormat = null;
        if (planningDate.length() == 7) {
            dateFormat = new SimpleDateFormat("yyyy-MM");
        } else if (planningDate.length() == 10) {
            dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        }
        Date planningDateFormat = null;
        try {
            planningDateFormat = dateFormat.parse(planningDate);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        if (planningDateFormat == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(planningDateFormat);
        return calendar;
    }
}
